package com.hongning.railway.ui.main.mine.score;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hongning.railway.BR;
import com.hongning.railway.R;
import com.hongning.railway.app.Constant;
import com.hongning.railway.databinding.FragmentRankBinding;
import com.hongning.railway.entity.resbean.ResScoreRankInfo;
import com.hongning.railway.ui.adapter.BaseFragmentPagerAdapter;
import com.hongning.railway.ui.main.fragments.college.CollegeExamFrament;
import com.hongning.railway.ui.main.fragments.college.CollegeLearnFragment;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;

import java.util.ArrayList;
import java.util.Arrays;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.base.BaseViewModel;

public class RankFragment extends BaseFragment<FragmentRankBinding, BaseViewModel> {
    private String title;
    private ResScoreRankInfo mResScoreRankInfo;
    private TimePickerDialog mDialogAll;
    long tenYears = 1L * 365 * 1000 * 60 * 60 * 24L;
    ArrayList<Fragment> fragments=new ArrayList<>();
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_rank;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {
        super.initParam();
        title = getArguments().getString(Constant.EXTRA_STRING);
        mResScoreRankInfo = getArguments().getParcelable(Constant.EXTRA_DATA);
    }

    @Override
    public void initData() {
        super.initData();
        mDialogAll = new TimePickerDialog.Builder()
                .setCallBack(new OnDateSetListener() {
                    @Override
                    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                        RankTypeFragment rankTypeFragment= (RankTypeFragment) fragments.get(binding.vpContent.getCurrentItem());
                        rankTypeFragment.filterbydata(millseconds);
                    }
                })
                .setCancelStringId("取消")
                .setSureStringId("确定")
                .setTitleStringId("时间选择")
                .setYearText("年")
                .setMonthText("月")
                .setCyclic(false)
                .setMinMillseconds(System.currentTimeMillis()-tenYears)
                .setMaxMillseconds(System.currentTimeMillis() + tenYears)
                .setCurrentMillseconds(System.currentTimeMillis())
                .setThemeColor(getResources().getColor(R.color.colorPrimary))
                .setType(Type.YEAR_MONTH)
                .setWheelItemTextNormalColor(getResources().getColor(R.color.timetimepicker_default_text_color))
                .setWheelItemTextSelectorColor(getResources().getColor(R.color.timepicker_toolbar_bg))
                .setWheelItemTextSize(12)
                .build();
        binding.appTitle.tvTopTitle.setText(title);
        binding.appTitle.ivLeft.setVisibility(View.VISIBLE);
        binding.appTitle.tvRight.setVisibility(View.VISIBLE);
        binding.appTitle.tvRight.setText("筛选");
        binding.appTitle.ivLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        binding.appTitle.tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialogAll.show(getChildFragmentManager(), "year_month");
            }
        });

        initViewPager();
    }

    private void initViewPager() {
        String[] title={"总分","红宁积分"};
        if ("总排行".equals(title)){
            fragments.add(RankTypeFragment.newInstance(mResScoreRankInfo,1));
            fragments.add(RankTypeFragment.newInstance(mResScoreRankInfo,2));
        }else {
            fragments.add(RankTypeFragment.newInstance(mResScoreRankInfo,3));
            fragments.add(RankTypeFragment.newInstance(mResScoreRankInfo,4));
        }

        BaseFragmentPagerAdapter pagerAdapter = new BaseFragmentPagerAdapter(getChildFragmentManager(), fragments, Arrays.asList(title));
        binding.vpContent.setAdapter(pagerAdapter);
        binding.stlTitle.setViewPager(binding.vpContent,title);
    }
}
